﻿using Simulation.Framework;
using UnityEngine.Events;
using UnityEngine.UI;
using UnityEngine;
using System.Collections.Generic;
using System.Collections;
using UnityEngine.EventSystems;

namespace Easy.Game2d
{

    public partial class UIStartPanel : PanelBase
    {

        #region override
        public override void Init(params object[] data)
        {
            InitComponent();
            AddBGClickEvent();
        }

        public override void Clear()
        {
            ClearComponent();
        }

        public override void Open()
        {
            _clickValue = 0;
            ClickStrValue();
            gameObject.SetActive(true);
        }

        public override void Close()
        {
            ContentTextTextMeshProUGUI.text = "";
            gameObject.SetActive(false);
        }
        #endregion

        #region data
        private int _clickValue = 0;
        private StorybackgroundDatas _storybackgroundDatas;
        #endregion

        #region  event

        private void AddBGClickEvent()
        {
            _storybackgroundDatas = new StorybackgroundDatas();
            EventTrigger trigger = BGEventTrigger;
            EventTrigger.Entry entry = new EventTrigger.Entry();
            entry.eventID = EventTriggerType.PointerDown;
            entry.callback.AddListener((data) => { OnPointerDownDelegate((PointerEventData)data); });
            trigger.triggers.Add(entry);
        }
        public void ClearBGClickEvent()
        {
            EventTrigger trigger = BGEventTrigger;
            EventTrigger.Entry entry = new EventTrigger.Entry();
            trigger.triggers.Clear();
        }

        #endregion
        #region logic
        private void OnPointerDownDelegate(PointerEventData data)
        {
            ClickStrValue();
        }

        private void ClickStrValue()
        {
            if (_clickValue >= 4)
            {
                Close();
                UIDialoguePanel dialoguePanel= SingleBehavior<PanelManager>.getInstance().Open<UIDialoguePanel>(PanelName._dialoguePanel);
                dialoguePanel.AddBGClickEvent();
                dialoguePanel.DialogueStep01();
                return;
            }
            if (_clickValue < _storybackgroundDatas._strArray.Length) ContentTextTextMeshProUGUI.text += "<color=#ffffff00>游戏</color>" + _storybackgroundDatas._strArray[_clickValue] + "\n";
            LayoutRebuilder.ForceRebuildLayoutImmediate(ContentTextTextMeshProUGUI.GetComponent<RectTransform>());
            LayoutRebuilder.ForceRebuildLayoutImmediate(ContentRectTransform.GetComponent<RectTransform>());
            TextScrollViewScrollRect.verticalNormalizedPosition = 0f;
            _clickValue += 1;
        }
        #endregion
    }
}
